<template>
  <el-form class="password-form" ref="form" :model="formData" :rules="submitRules" label-width="120px">
    <el-form-item label="旧密码" prop="old_password">
      <el-input type="password" v-model="formData.old_password" />
    </el-form-item>
    <el-form-item label="新密码" prop="new_password">
      <el-input type="password" v-model="formData.new_password" />
    </el-form-item>
    <el-form-item label="确认密码" prop="againPassword">
      <el-input type="password" v-model="formData.againPassword" />
    </el-form-item>
    <el-form-item label=" ">
      <el-button class="none-type" @click="commit" type="primary">确认</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { password } from '@/api/login'
import { encrypt } from '@/utils/rsaEncrypt'
export default {
  data() {
    return {
      formData: {},
      submitRules: {
        old_password: [
          { required: true, message: "该项为必填项" },
          { required: true, min: 1, max: 25, message: "长度在1到25个字符" },
        ],
        new_password: [
          { required: true, message: "该项为必填项" },
          { required: true, min: 6, max: 25, message: "长度在6到25个字符" },
        ],
        againPassword: [
          { required: true, message: "该项为必填项" },
          { required: true, min: 6, max: 25, message: "长度在6到25个字符" },
        ]
      }
    };
  },
  async created() {

  },

  async mounted() {

  },
  destroyed() {

  },
  methods: {
    async commit() {
      if (this.formData.new_password != this.formData.againPassword) {
        alert("两次输入的密码不一致")
        return
      }
      let myInfo = await this.$myInfo(this)
      let old_password = encrypt(this.formData.old_password)
      let new_password = encrypt(this.formData.new_password)
      //let phone = encrypt(myInfo.phone)
      let data={
        'phone': myInfo.username,
        'old_password':old_password,
        'new_password':new_password,
      }
      let res = await password(data)
      if (res.code == 200){
        this.$message({
          showClose: true,
          message: '密码修改成功',
          type: 'success'
        });
      }

    }
  }
}
</script>
<style lang="less" scoped>
.password-form {
  margin: 16px 0;
}
</style>
